<?php
// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2018~2018 GoMaoMao [ http://www.gomaomao.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.gomaomao.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | 联系方式：GoMaoMao <vip.vvm@qq.com>
// +----------------------------------------------------------------------
// | github开源项目：https://gitee.com/gomaomao.com/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\miniprogram\controller;

use controller\BasicAdmin;
use service\DataService;
use service\ToolsService;
use think\Db;

/**
 * 商店内容管理
 * Class Goods
 * @package app\store\controller
 * @author Anyon <zoujingli@qq.com>
 * @date 2017/03/27 14:43
 */
class Document extends BasicAdmin
{

	/**
	 * 定义当前操作表名
	 * @var string
	 */
	public $table = 'mp_document';

	/**
	 * 普通内容
	 * @return array|string
	 * @throws \think\Exception
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function index()
	{
		$this->title = '内容管理';
		$get         = $this->request->get();
		$db          = Db::name($this->table);
		$db->where('website', session('website'));
		foreach (['tags_id', 'title'] as $field) {
			(isset($get[$field]) && $get[$field] !== '') && $db->whereLike($field, "%{$get[$field]}%");
		}
		foreach (['recommend', 'status', 'cate_id'] as $field) {
			(isset($get[$field]) && $get[$field] !== '') && $db->where($field, $get[$field]);
		}
		if (isset($get['create_at']) && $get['create_at'] !== '') {
			list($start, $end) = explode(' - ', $get['create_at']);
			$db->whereBetween('create_at', ["{$start} 00:00:00", "{$end} 23:59:59"]);
		}
		return parent::_list($db->order('sort asc,id desc'));
	}

	/**
	 * 数据处理
	 * @param array $data
	 */
	protected function _index_data_filter(&$data)
	{
		foreach ($data as $k => $vo) {
			$cate                   = Db::name('mp_document_cate')->where('id = ' . $vo['cate_id'])->cache(10)->find();
			$data[$k]['cate_title'] = $cate['cate_title'];
		}
		$data      = ToolsService::arr2table($data);
		$cateField = 'id,pid,cate_title,cate_desc';
		$cateWhere = ['status' => '1', 'is_deleted' => '0'];
		$cateList  = Db::name('mp_document_cate')->where($cateWhere)->order('sort asc,id asc')->column($cateField);
		$cates     = ToolsService::arr2table($cateList);
		$this->assign('cates', $cates);
	}

	/**
	 * 添加
	 * @return array|string
	 * @throws \think\Exception
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function add()
	{
		return $this->_form($this->table, 'form');
	}

	/**
	 * 编辑
	 * @return array|string
	 * @throws \think\Exception
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function edit()
	{
		return $this->_form($this->table, 'form');
	}


	/**
	 * 表单数据处理
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	protected function _form_filter(&$vo)
	{
		if ($this->request->isGet()) {
			list($where, $order) = [['status' => '1', 'is_deleted' => '0',], 'sort asc,id desc'];
			$Cate = Db::name('mp_document_cate');
			$Cate->where('website', session('website'));
			$cates = (array)$Cate->where($where)->order($order)->select();
			// 所有的内容信息
			// $where          = ['is_deleted' => '0', 'status' => '1'];
			$this->assign([
					'cates' => ToolsService::arr2table($cates),
			]);
		}
		$vo['website']   = session('website');
		$vo['uid']       = session('user.id');
		$vo['update_at'] = time();

	}

	/**
	 * 删除内容
	 * @throws \think\Exception
	 * @throws \think\exception\PDOException
	 */
	public function del()
	{
		if (DataService::update($this->table)) {
			$this->success("内容删除成功！", '');
		}
		$this->error("内容删除失败，请稍候再试！");
	}

	/**
	 * 内容禁用
	 * @throws \think\Exception
	 * @throws \think\exception\PDOException
	 */
	public function forbid()
	{
		if (DataService::update($this->table)) {
			$this->success("内容禁用成功！", '');
		}
		$this->error("内容禁用失败，请稍候再试！");
	}

	/**
	 * 内容禁用
	 * @throws \think\Exception
	 * @throws \think\exception\PDOException
	 */
	public function resume()
	{
		if (DataService::update($this->table)) {
			$this->success("内容启用成功！", '');
		}
		$this->error("内容启用失败，请稍候再试！");
	}

	public function recommend()
	{
		if (DataService::update($this->table)) {
			$this->success("操作成功！", '');
		}
		$this->error("操作失败，请稍候再试！");
	}

}
